package com.teatrace.web.module;

import java.util.HashMap;
import java.util.Map;

import org.nutz.json.JsonFormat;
import org.nutz.mvc.View;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.view.UTF8JsonView;

import com.teatrace.model.AntiFakeCode;
import com.teatrace.service.AntiFakeService;
import com.teatrace.web.msg.Msg;
import com.teatrace.webservice.client.WebServiceUtils;

@At("/antifake")
public class ValidateModule {

	@At("/validate")
	public View validate(@Param("code") String code) {
		Map msg = this.getValidatedMsgMap(code);
		UTF8JsonView view = new UTF8JsonView(JsonFormat.compact());
		view.setData(msg);
		return view;
	}
	
	public Map<String, Object> getValidatedMsgMap(String code) {
		AntiFakeService service = (AntiFakeService) WebServiceUtils.getService("antiFakeService");
		AntiFakeCode antiFakeCode = service.get(code.trim());
		Map<String, Object> msg = new HashMap<String, Object>();
		if(null != antiFakeCode && antiFakeCode.isApply()) {
			int count = antiFakeCode.getValidateCount();
			if(count > 0) {
				msg.put("msg", Msg.getResource("antiFakeCodeValidateCountGreatThanOne"));
			} else {
				msg.put("msg", Msg.getResource("antiFakeCodeValidateSuccess"));
			}
			count ++;
		}
		else {
			msg.put("msg", Msg.getResource("antiFakeCodeValidateFail"));
		}
		return msg;
	}
}
